local DB = require("common.DB")
local Util = require("common.Util")
local LuaMongo = _G.lua_mongo
local NoticeExcel = require("excel.Notice")
local Human = require("core.Human")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local RoleHandlerW = require("role.HandlerW")
local OperateActFW = require("activity.OperateActFW")
local ComplexSrvActFW = require("activity.ComplexSrvActFW") 
local DateActFW = require("activity.DateActFW") 


serverCommon_Id = serverCommon_Id or "serverCommon"
ServerNotice = ServerNotice or ""
NewServerNotice = NewServerNotice or ""
ServerVersion = ServerVersion or ""
NewServerVersion = NewServerVersion or ""
ServerOpenTime = ServerOpenTime or 0
ComplexSrvTime = ComplexSrvTime or 0
VersionTime = VersionTime or 0
VERSION_TIME_LOCAL = VERSION_TIME_LOCAL or nil
HAD_OPEN_DAY = HAD_OPEN_DAY or nil 
HAD_COMPLEXSRV_DAY = HAD_COMPLEXSRV_DAY or nil

local tb = {}
function upDateServerCommon(upDate)
    upDate.serverCommon_Id = serverCommon_Id
    tb["$set"] = upDate
    LuaMongo.update(DB.db_server, {serverCommon_Id = serverCommon_Id}, tb, true)
end

function onGetServerCommonDB()
    LuaMongo.find(DB.db_server, {serverCommon_Id = serverCommon_Id})
    local nReader = {}
    if not LuaMongo.next(nReader) then
        return nil
    end
    return nReader
end

function setFirstOpenTime(serverOpenTime)
    ServerOpenTime = serverOpenTime
    getHadOpenDay(true)
    upDateServerCommon({ServerOpenTime = serverOpenTime})
    onSend2LOpenServerTime(serverOpenTime)
    return true
end

function setComplexSrvTime(complexSrvTime)
    ComplexSrvTime = complexSrvTime
    getHadComplexSrvDay(true)
    upDateServerCommon({ComplexSrvTime = complexSrvTime})
    onSend2LComplexSrvTime(complexSrvTime)
    return true
end

function onSend2LOpenServerTime()
    local nSend2Ldata = {
        type = 1,
        data = {
            serverOpenTime = ServerOpenTime
        }
    }

    InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, nSend2Ldata)
end

function onSend2LComplexSrvTime()
    local nSend2Ldata = {
        type = 2,
        data = {
            complexSrvTime = ComplexSrvTime 
        }
    }

    InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, nSend2Ldata)
end

function setServerNotice(notice, ctlTime)
    NewServerNotice = notice
    VersionTime = ctlTime or os.time()
    if VersionTime <= os.time() then
        ServerNotice = NewServerNotice
        NewServerNotice = ""
        for k, v in pairs(Human.onlineUuid) do
            if v.fd then
                RoleHandlerW.CG_SERVER_NOTICE(v, {type = 1}) 
            end
        end
    end
    upDateServerCommon({ServerNotice = ServerNotice, NewServerNotice = notice, VersionTime = VersionTime})
end

function setServerVersion(version, ctlTime)
    NewServerVersion = version
    VersionTime = ctlTime or os.time()
    if VersionTime <= os.time() then
        ServerVersion = NewServerVersion
        NewServerVersion = ""
    end
    upDateServerCommon({ServerVersion = ServerVersion, NewServerVersion = version, VersionTime = VersionTime})
end

function getFirstOpenTime() --得到服务器启动时间
    return ServerOpenTime
end

function getComplexSrvTime()
    return ComplexSrvTime
end

function getHadOpenDay(init)            --得到已开服多少天
    if init or HAD_OPEN_DAY == nil then
        if ServerOpenTime == 0 then 
            HAD_OPEN_DAY = 0
            return HAD_OPEN_DAY
        end

        local nTimeStamp = os.time()
        local nToday = os.date("%Y%m%d", nTimeStamp)
        local nServerOpenDay = os.date("%Y%m%d", getFirstOpenTime())
        if nServerOpenDay > nToday then
            HAD_OPEN_DAY = 0
        else
            local nServerOpenTime = getFirstOpenTime()
            local nServerOpenDateTime = os.date("*t", nServerOpenTime)
            nServerOpenDateTime.hour = 0
            nServerOpenDateTime.min = 0
            nServerOpenDateTime.sec = 0

            local nTodayDateTime = os.date("*t", nTimeStamp)
            nTodayDateTime.hour = 0
            nTodayDateTime.min = 0
            nTodayDateTime.sec = 0

            local nTimeDiff = os.time(nTodayDateTime) - os.time(nServerOpenDateTime)
            HAD_OPEN_DAY =  math.ceil(nTimeDiff / 86400) + 1
        end
    end
--    print("had open day ", HAD_OPEN_DAY)
    return HAD_OPEN_DAY
end

function getHadComplexSrvDay(init)
    if init or HAD_COMPLEXSRV_DAY == nil then
        if ComplexSrvTime == 0 or not ComplexSrvTime then 
            HAD_COMPLEXSRV_DAY = 0
            return HAD_COMPLEXSRV_DAY
        end

        local nTimeStamp = os.time()
        local nToday = os.date("%Y%m%d", nTimeStamp)
        local nServerComplexDay = os.date("%Y%m%d", getComplexSrvTime())
        if nServerComplexDay > nToday then
            HAD_COMPLEXSRV_DAY = 0
        else
            local nServerComplexTime = getComplexSrvTime()
            local nServerComplexDateTime = os.date("*t", nServerComplexTime)
            nServerComplexDateTime.hour = 0
            nServerComplexDateTime.min = 0
            nServerComplexDateTime.sec = 0

            local nTodayDateTime = os.date("*t", nTimeStamp)
            nTodayDateTime.hour = 0
            nTodayDateTime.min = 0
            nTodayDateTime.sec = 0

            local nTimeDiff = os.time(nTodayDateTime) - os.time(nServerComplexDateTime)
            HAD_COMPLEXSRV_DAY =  math.ceil(nTimeDiff / 86400) + 1
        end
    end
--    print("had complex srv day ", HAD_COMPLEXSRV_DAY)
    return HAD_COMPLEXSRV_DAY
end

function getServerNotice()
    if VersionTime <= os.time() and NewServerNotice ~= "" and ServerNotice ~= NewServerNotice then
        ServerNotice = NewServerNotice 
        NewServerNotice = ""
        upDateServerCommon({ServerNotice = ServerNotice, NewServerNotice = NewServerNotice, VersionTime = VersionTime})
    end
    return ServerNotice, NewServerNotice
end

function getServerVersion()
    if VersionTime <= os.time() and NewServerVersion ~= "" and ServerVersion ~= NewServerVersion then
        ServerVersion = NewServerVersion
        NewServerVersion = ""
        upDateServerCommon({ServerVersion = ServerVersion, NewServerVersion = NewServerVersion, VersionTime = VersionTime})
    end
    return ServerVersion, NewServerVersion
end

function getVersionTime()
    return VersionTime
end

function getVersionTimeLocal()
	if VERSION_TIME_LOCAL == nil then
		local temp = {}
		temp.year = NoticeExcel.update[1].updateTime[1]
		temp.month = NoticeExcel.update[1].updateTime[2]
		temp.day = NoticeExcel.update[1].updateTime[3]
		temp.hour = NoticeExcel.update[1].updateTime[4]
		temp.min = NoticeExcel.update[1].updateTime[5]
		temp.sec = NoticeExcel.update[1].updateTime[6]
		
		VERSION_TIME_LOCAL = os.time(temp)
	end

	return VERSION_TIME_LOCAL
end

function onInit()
    local nResult = onGetServerCommonDB()
    if not nResult then
        --setFirstOpenTime(os.time())
        ServerOpenTime = 0
    else
        ServerOpenTime = nResult.ServerOpenTime
        if ServerOpenTime == nil then 
            ServerOpenTime = 0 
        end
        print("init ServerOpenTime", ServerOpenTime)
        VersionTime = nResult.VersionTime or 0 
        ServerNotice = nResult.ServerNotice
        NewServerNotice = nResult.NewServerNotice
        ServerVersion = nResult.ServerVersion
        NewServerVersion = nResult.NewServerVersion
        ComplexSrvTime = nResult.ComplexSrvTime
        onSend2LOpenServerTime(serverOpenTime)
    end
end

function L2WProcess(human, type, nData)
    if nData.type == 1 then
        setServerNotice(nData.data.notice, os.time())
    elseif nData.type == 2 then
        setServerVersion(nData.data.version, os.time())
    elseif nData.type == 3 then
        setFirstOpenTime(nData.data)
        OperateActFW.onInitTodayAct()
        OperateActFW.onActCheck()
    elseif nData.type == 4 then
        setComplexSrvTime(nData.data)
        ComplexSrvActFW.onInitTodayAct()
        ComplexSrvActFW.onActCheck()
    elseif nData.type == 5 then
        setComplexSrvTime(nData.data)
        DateActFW.onInitTodayAct()
        DateActFW.onActCheck()
    end
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, L2WProcess)
